package cn.yuemouren.suanfa;

/**
 * @Author: Timi
 * @Description: 二分查找
 * @Date: 2020/7/23 11:04
 */
public class BinarySearch {

    public static void main(String[] args) {
        int arr[] = {1,3,4,5,6,8,10,24,35};
        int i = 6;
        System.out.println(search(arr,i));
    }

    private static int search(int[] arr,int i){
        int lo = 0;
        int ro = arr.length - 1;
        int mid;
        while(lo < ro){
            //mid = (lo + ro) / 2;
            mid = (lo + (ro - lo)) / 2;
            if(arr[mid] == i){
                return mid;
            } else if(i < arr[mid]){
                ro = mid - 1;
            } else {
                lo = mid + 1;
            }
        }
        return -1;
    }
}
